package com.huohuzhihui.web.controller.api;

import com.huohuzhihui.api.service.ApiMerchantService;
import com.huohuzhihui.common.annotation.Log;
import com.huohuzhihui.common.core.controller.BaseController;
import com.huohuzhihui.common.core.domain.AjaxResult;
import com.huohuzhihui.common.core.domain.entity.SysDictData;
import com.huohuzhihui.common.core.domain.entity.SysUser;
import com.huohuzhihui.common.core.domain.model.LoginUser;
import com.huohuzhihui.common.enums.BusinessType;
import com.huohuzhihui.common.utils.DateUtils;
import com.huohuzhihui.common.utils.ServletUtils;
import com.huohuzhihui.common.utils.StringUtils;
import com.huohuzhihui.common.utils.ip.AddressUtils;
import com.huohuzhihui.common.utils.ip.IpUtils;
import com.huohuzhihui.framework.web.service.TokenService;
import com.huohuzhihui.merchant.domain.*;
import com.huohuzhihui.merchant.service.IMerActivityBrowseService;
import com.huohuzhihui.merchant.service.IMerCouponActivityService;
import com.huohuzhihui.merchant.service.IMerCouponService;
import com.huohuzhihui.merchant.service.IMerDouyinService;
import com.huohuzhihui.system.service.ISysConfigService;
import com.huohuzhihui.system.service.ISysDictDataService;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 营销活动
 *
 * @author lizhihe
 * @date 2020-11-22
 */
@RestController
@RequestMapping("/api/activity")
public class ApiActivityController extends BaseController {
    @Autowired
    private IMerActivityBrowseService merActivityBrowseService;
    @Autowired
    private IMerCouponActivityService merCouponActivityService;
    @Autowired
    private IMerCouponService merCouponService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private IMerDouyinService merDouyinService;

    /**
     * 查询活动信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getActivity")
    public AjaxResult getActivity(Long id) {
        MerCouponActivity merMerchantActity = this.merCouponActivityService.selectMerCouponActivityById(id);
        Date endDate = merMerchantActity.getEndDate();
        Date now = new Date();
        if (now.compareTo(endDate) > 0) {//活动过期自动下线
            merMerchantActity.setStatus(3L);
            this.merCouponActivityService.updateMerCouponActivity(merMerchantActity);
        }
        //查询优惠券
        MerCoupon merCoupon = this.merCouponService.selectMerCouponById(merMerchantActity.getCouponId());
        merMerchantActity.setMerCoupon(merCoupon);

        //如果是跳转到个人主页，那么去设置个人主页
        String redirectType = merMerchantActity.getRedirectType();
        if ("1".equals(redirectType.trim())) {
            MerDouyin merDouyin = this.merDouyinService.selectMerDouyinByDeptId(merMerchantActity.getMerchantId());
            String douyinUrl = merDouyin.getDouyinUrl();
            if (!StringUtils.isEmpty(douyinUrl)) {
                merMerchantActity.setRemark(merDouyin.getDouyinUrl());
            }
        }
        return AjaxResult.success("查询活动详情成功", merMerchantActity);
    }

    /**
     * 查询上线中活动信息
     *
     * @param
     * @return
     */
    @GetMapping("/findOnlineActivity")
    public AjaxResult findOnlineActivity(HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getUser();
        MerCouponActivity merMerchantActity = new MerCouponActivity();
        merMerchantActity.setMerchantId(sysUser.getDeptId());
        merMerchantActity.setStatus(2L);

        List<MerCouponActivity> merCouponActivityList = this.merCouponActivityService.selectMerCouponActivityList(merMerchantActity);
        //只允许有一个上线中活动
        if (merCouponActivityList != null && merCouponActivityList.size() > 0) {
            MerCouponActivity activity = merCouponActivityList.get(0);

            String url = configService.selectConfigByKey("activity_url");
            url = url + "?activityId=" + activity.getId() + "&merchantId=" + activity.getMerchantId();
            //设置活动二维码字符串
            activity.setQrCodeText(url);

            return AjaxResult.success("查询商户活动信息成功", activity);
        } else {
            return AjaxResult.success("查询商户活动信息成功", null);
        }
    }


    /**
     * 查询活动信息
     *
     * @param
     * @return
     */
    @GetMapping("/findActivityList")
    public AjaxResult findActivityList(HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getUser();
        MerCouponActivity merMerchantActity = new MerCouponActivity();
        merMerchantActity.setMerchantId(sysUser.getDeptId());
        return AjaxResult.success("查询商户活动信息成功", this.merCouponActivityService.selectMerCouponActivityList(merMerchantActity));
    }


    /**
     * 插入一条活动浏览记录
     *
     * @param merchantId
     * @param activityId
     * @return
     */
    @GetMapping("/saveBrowseRecord")
    @ResponseBody
    public AjaxResult saveBrowseRecord(Long merchantId, Long activityId) {
        //插入浏览记录
        MerActivityBrowse merActivityBrowse = new MerActivityBrowse();
        merActivityBrowse.setMerchantId(merchantId);
        merActivityBrowse.setActivityId(activityId);
        merActivityBrowse.setCreateTime(DateUtils.getNowDate());

        String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
        merActivityBrowse.setIp(ip);
        merActivityBrowse.setLocation(AddressUtils.getRealAddressByIP(ip));

        UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
        merActivityBrowse.setBrowser(userAgent.getBrowser().getName());
        merActivityBrowse.setOs(userAgent.getOperatingSystem().getName());
        merActivityBrowseService.insertMerActivityBrowse(merActivityBrowse);

        return AjaxResult.success();
    }

    /**
     * 新增优惠券活动
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody MerCouponActivity merCouponActivity,HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getUser();
        merCouponActivity.setMerchantId(sysUser.getDeptId());
        merCouponActivity.setCreateBy(sysUser.getUserName());
        merCouponActivity.setCreateTime(new Date());
        return toAjax(merCouponActivityService.insertMerCouponActivity(merCouponActivity));
    }

    /**
     * 修改优惠券活动
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody MerCouponActivity merCouponActivity,HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getUser();
        merCouponActivity.setUpdateBy(sysUser.getUserName());
        merCouponActivity.setUpdateTime(new Date());
        return toAjax(merCouponActivityService.updateMerCouponActivity(merCouponActivity));
    }
}
